package com.sharedmobility.servlet;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sharedmobility.dao.UserDAO;
import com.sharedmobility.model.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.lang.reflect.Type;

@WebServlet("/api/user/update")
public class UserUpdateServlet extends HttpServlet {
    private UserDAO userDAO;
    private Gson gson;
    private static final Type MAP_TYPE = new TypeToken<Map<String, Object>>(){}.getType();

    @Override
    public void init() throws ServletException {
        userDAO = new UserDAO();
        gson = new Gson();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        try {
            // 获取当前登录用户ID
            HttpSession session = request.getSession();
            Integer userId = (Integer) session.getAttribute("userId");
            
            if (userId == null) {
                throw new Exception("用户未登录");
            }

            // 获取请求参数
            Map<String, Object> requestData = gson.fromJson(request.getReader(), MAP_TYPE);
            String username = (String) requestData.get("username");
            String phone = (String) requestData.get("phone");
            String currentPassword = (String) requestData.get("currentPassword");
            String newPassword = (String) requestData.get("newPassword");

            // 获取用户信息
            User user = userDAO.findById(userId);
            if (user == null) {
                throw new Exception("用户不存在");
            }

            // 验证当前密码
            if (!user.getPassword().equals(currentPassword)) {
                throw new Exception("当前密码错误");
            }

            // 更新用户信息
            user.setUsername(username);
            user.setPhone(phone);
            if (newPassword != null && !newPassword.isEmpty()) {
                user.setPassword(newPassword);
            }

            // 保存更新
            boolean success = userDAO.update(user);
            if (!success) {
                throw new Exception("更新用户信息失败");
            }

            // 返回成功响应
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "个人信息修改成功");
            
            response.getWriter().write(gson.toJson(result));
        } catch (Exception e) {
            // 返回错误响应
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", e.getMessage());
            
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().write(gson.toJson(result));
        }
    }
} 